package org.shuker.sqlsession.impl;

import org.shuker.pojo.Configuration;
import org.shuker.pojo.MappedStatement;
import org.shuker.executor.Executor;
import org.shuker.executor.impl.SimpleExecutor;
import org.shuker.sqlsession.SqlSession;

import java.lang.reflect.*;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * @author ：Shuker
 * @date ：Created in 2022/6/6 11:50
 * @description：SqlSession实现类
 * @version: 1.0
 */
public class DefaultSqlSession implements SqlSession {

    private Configuration configuration;
    //        处理器对象
    private Executor simpleExecutor = new SimpleExecutor();

    public DefaultSqlSession(Configuration configuration) {
        this.configuration = configuration;
    }

    @Override
    public <E> List<E> selectList(String statementId, Object... param) throws Exception {
        MappedStatement mappedStatement = configuration.getMappedStatementMap().get(statementId);
        List<E> query = simpleExecutor.query(configuration, mappedStatement, param);
        return query;
    }

    @Override
    public <T> T selectOne(String statementId, Object... param) throws Exception {
        List<Object> objects = selectList(statementId, param);
        if (objects.size() == 1) {
            return (T) objects.get(0);
        } else {
            throw new RuntimeException("返回结果过多");
        }
    }

    @Override
    public int add(String statementId, Object... param) throws Exception {
        MappedStatement mappedStatement = configuration.getMappedStatementMap().get(statementId);
        int i = simpleExecutor.update(configuration, mappedStatement, param);
        return i;
    }

    @Override
    public int update(String statementId, Object... param) throws Exception {
        MappedStatement mappedStatement = configuration.getMappedStatementMap().get(statementId);
        int i = simpleExecutor.update(configuration, mappedStatement, param);
        return i;
    }

    @Override
    public int delete(String statementId, Object... param) throws Exception {
        MappedStatement mappedStatement = configuration.getMappedStatementMap().get(statementId);
        int i = simpleExecutor.update(configuration, mappedStatement, param);
        return i;
    }

    @Override
    public <T> T getMapper(Class<?> mapperClass) {
        T o = (T) Proxy.newProxyInstance(DefaultSqlSession.class.getClassLoader(), new Class[]{mapperClass}, new InvocationHandler() {
            @Override
            public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
//                selectOne
                String methodName = method.getName();
//                className:namespace
                String className = method.getDeclaringClass().getName();
//                statementId
                String key = className + "." + methodName;
                Type genericReturnType = method.getGenericReturnType();
                ArrayList arrayList = new ArrayList();
//                判断是否是添加方法
                if (methodName == "add") {
                    return add(key, args);
                }
//                判断是否是更新方法
                if (methodName == "update") {
                    return update(key, args);
                }
//                判断是否是删除方法
                if (methodName == "delete") {
                    return delete(key, args);
                }

//                判断是否实现泛型类型参数化
                if (genericReturnType instanceof ParameterizedType) {
                    List<T> o = selectList(key, args);
                    return o;
                }
                return selectOne(key, args);
            }
        });
        return o;
    }

    @Override
    public void close() throws SQLException {
        simpleExecutor.close();
    }
}
